package edu.byu.isys413.samlam;

import java.sql.*;
import java.util.*;

/**
 * A simple tester class for my data layer.
 *
 * @author Group 2g
 */
public class Tester {
  
  public static void main(String args[]) {
    try {

      // clear out the database (you'd never do this in production)
      Connection conn = ConnectionPool.getInstance().get();
      Statement stmt = conn.createStatement();
      stmt.executeUpdate("DELETE FROM customer");
      stmt.executeUpdate("DELETE FROM areaofinterest");
      stmt.executeUpdate("DELETE FROM member");
      stmt.executeUpdate("DELETE FROM member_areaofinterest");
      stmt.close();
      conn.commit();
      ConnectionPool.getInstance().release(conn);
      
      // create areas of interest
      //AreaOfInterest aoi = AreaOfInterestDAO.getInstance().create();
      
      // create customer
      // create member
      // add areas of interest to member
      
      
      
      // create a customer
      Customer cust = CustomerDAO.getInstance().create();
      cust.setFirstName("Homer");
      cust.setLastName("Simpson");
      cust.setEmail("samlambson@hotmail.com");
      cust.setAddress("742 Evergreen Terrace");
      cust.setCity("Springfield");
      cust.setState("Unknown");
      cust.setZipCode("84235");
      cust.setPhone("801-224-0233");
      CustomerDAO.getInstance().save(cust);
      
      // create a second customer
      Customer cust2 = CustomerDAO.getInstance().create();
      cust2.setFirstName("Marge");
      cust2.setLastName("Simpson");
      cust2.setEmail("nick@byu.edu");
      cust2.setAddress("742 Evergree Terrace");
      cust2.setCity("Unknown");
      cust2.setState("MN");
      cust2.setZipCode("84602");
      cust2.setPhone("801-224-0233");
      CustomerDAO.getInstance().save(cust2);
     
      // get the customers on the account
      System.out.println("Customer 1: " + cust.getFirstName() + " " + cust.getLastName() + "\n\tAddress: " + cust.getAddress() + ", " + cust.getCity() + ", " + cust.getState() + "  " + cust.getZipCode() + "\n\tEmail: " +cust.getEmail() + "\tPhone:" + cust.getPhone()  + "\n\tId: " + cust.getId()+ "\n\tMembership: " + cust.getMember());
      System.out.println("Customer 2: " + cust2.getFirstName() + " " + cust2.getLastName() + "\n\tAddress: " + cust2.getAddress() + ", " + cust2.getCity() + ", " + cust2.getState() + "  " + cust2.getZipCode() + "\n\tEmail: " +cust2.getEmail() + "\tPhone:" + cust2.getPhone()  + "\n\tId: " + cust2.getId() + "\n\tMembership: " + cust2.getMember());
      
      // reload everything from scratch to test whether the read methods work
      // this essentially is like restarting the entire system since 
      // we're going to clear the cache out
      Cache.getInstance().clear(); // you'd never clear the cache in production (we do so for testing)
      cust2 = CustomerDAO.getInstance().read(cust2.getId());
      System.out.println("Customer 2 taken from the DB: " + cust2.getFirstName() + " " + cust2.getLastName() + "\n\tAddress: " + cust2.getAddress() + ", " + cust2.getCity() + ", " + cust2.getState() + "  " + cust2.getZipCode() + "\n\tEmail: " +cust2.getEmail() + "\tPhone:" + cust2.getPhone()  + "\n\tId: " + cust2.getId() + "\n\tMembership: " + cust2.getMember());
      
      //update the object and save
      cust2.setLastName("Lambson");
      cust2.setAddress("2503 E. Gemeni");
      cust2.setCity("Gilbert");
      cust2.setEmail("samlambson@gmail.com");
      cust2.setFirstName("Sam");
      cust2.setPhone("(480)926-6435");
      cust2.setState("AZ");
      CustomerDAO.getInstance().save(cust2);
      
      Cache.getInstance().clear();
      cust2 = CustomerDAO.getInstance().read(cust2.getId());
      System.out.println("Customer 2 after changes and taken from the DB: " + cust2.getFirstName() + " " + cust2.getLastName() + "\n\tAddress: " + cust2.getAddress() + ", " + cust2.getCity() + ", " + cust2.getState() + "  " + cust2.getZipCode() + "\n\tEmail: " +cust2.getEmail() + "\tPhone:" + cust2.getPhone()  + "\n\tId: " + cust2.getId() + "\n\tMembership: " + cust2.getMember());
      
      List customerList = CustomerDAO.getInstance().getAll();// use later
      
      
      // create a membership to assign to my first customer object
      Member memb = MemberDAO.getInstance().create();
      memb.setCreditCard("3593346");
      memb.setExpmonth(12);
      memb.setExpyear(2010);
      memb.setBalance(-509485);
      memb.setCustomer(cust);
      cust.setMember(memb);
      CustomerDAO.getInstance().save(cust);
      //MemberDAO.getInstance().save(memb);
      
      Cache.getInstance().clear();
      cust = CustomerDAO.getInstance().read(cust.getId());
      System.out.println("Customer 1 with a member: " + cust.getFirstName() + " " + cust.getLastName() + "\n\tAddress: " + cust.getAddress() + ", " + cust.getCity() + ", " + cust.getState() + "  " + cust.getZipCode() + "\n\tEmail: " +cust.getEmail() + "\tPhone:" + cust.getPhone()  + "\n\tId: " + cust.getId()+ "\n\tMembership: " + cust.getMember());
      
      
      System.out.println("Customer 1's account balance is:"+ cust.getFirstName() + " " + memb.getBalance());
      
      //clear cache
      
      Cache.getInstance().clear();
      Member memb2 = MemberDAO.getInstance().readByCustomer(cust.getId());
      System.out.println("Member balance is in the DB: " + memb2.getBalance());
      
      AreaOfInterest aoi = AreaOfInterestDAO.getInstance().create();
      aoi.setDescription("Running");
      AreaOfInterest aoi2 = AreaOfInterestDAO.getInstance().create();
      aoi2.setDescription("Fighting");
      AreaOfInterestDAO.getInstance().save(aoi);
      AreaOfInterestDAO.getInstance().save(aoi2);
      Cache.getInstance().clear();
      
      System.out.println("Check out the areas of interest:"+AreaOfInterestDAO.getInstance().read(aoi.getId()).getDescription()+" and "+AreaOfInterestDAO.getInstance().read(aoi2.getId()).getDescription());
      LinkedList<AreaOfInterest> areaList = new LinkedList();
      areaList.add(aoi);
      areaList.add(aoi2);
      memb.setArealist(areaList);
      AreaOfInterestDAO.getInstance().save(areaList, memb.getId());
      Cache.getInstance().clear();
      
      
      List<Member> membList = MemberDAO.getInstance().getByAreaOfInterest(aoi);
      System.out.println("This is all of members that are assigned to the first Areaofinterest: ");
      Member membershizzle = membList.get(0);
      for(int i = 0; i < membList.size() ; i++){
          Member member = membList.get(i);
          System.out.println("Member #" + (i+1) + ": " + member.getCustomer().getFirstName() + " " + member.getCustomer().getLastName());
          System.out.println("\t Balance: " + member.getBalance());
          System.out.println("\t Credit Card Number: " + member.getCreditCard());
          System.out.println("\t Expiration Date: " + member.getExpmonth() + "/" + member.getExpyear());
          System.out.println("\t ID: " + member.getId());
      }
      
      List<AreaOfInterest> aoiList = AreaOfInterestDAO.getInstance().getByMember(memb.getId());
     System.out.print("This is all of AreaOfInterests that are assigned to the first Member: ");
      for(int i = 0; i < aoiList.size() ; i++){
          System.out.println(aoiList.get(i).getId());
      }
      
     /////////////////////////////////////////////////////////////////////
     /////// Milestone 2 tester starts here
     ///////////////////////////////////////////////////////////
     System.out.println("*****************************************\n** Milestone 2 Starts here\n****************************************");
      
      // create areas of interest
      //AreaOfInterest aoi = AreaOfInterestDAO.getInstance().create();
      
      // create customer
      // create member
      // add areas of interest to member
      
     
     Employee steve = EmployeeDAO.getInstance().create();
      steve.setFirstName("Steve");
      steve.setLastName("Dire");
      steve.setHireDate("1092364211773");
      steve.setPhone("(801)765-9876");
      steve.setPosition("cashier");
      steve.setSalary(35000.00);
      EmployeeDAO.getInstance().save(steve);
      Cache.getInstance().clear();
      steve = EmployeeDAO.getInstance().read(steve.getId());
      System.out.println("First Name: " + steve.getFirstName() + "\n Last Name: " + steve.getLastName() + "\n Hire Date: " + steve.getHireDate() +"\n Phone: "+ steve.getPhone()+ "\n Position: "+steve.getPosition()+"\n Salary: "+ steve.getSalary());
      
      Sale sale = SaleDAO.getInstance().create();
      sale.setProductID("3198HT6B");
      sale.setQuantity(2);
      sale.setDescription("this is a greate product");
      SaleDAO.getInstance().save(sale);
      Cache.getInstance().clear();
      sale = SaleDAO.getInstance().read(sale.getId());
      System.out.println("Sale:" +sale.getProductID() +"\n Quantity:" + sale.getQuantity());
      
      Vendor vendor = VendorDAO.getInstance().create();
        VendorDAO.getInstance().save(vendor);
        vendor.setPhone("343434");
        vendor.setAddress("4356 Unlucky Street");
        vendor.setContact("Amazing Grace");
        vendor.setName("Amazing Vendor");
        vendor.setPhone("987-987-9867");
        VendorDAO.getInstance().save(vendor);
        vendor = VendorDAO.getInstance().read(vendor.getVguid());
        System.out.println("Phone "+vendor.getPhone()+ "\n Address: " +vendor.getAddress()+ "\n Contact: " +vendor.getContact()+ "\n Name: " +vendor.getName()+ "\n Phone: " +vendor.getPhone());
        
        System.out.println("vendor id is "+vendor.getVguid());
      
        VendorItem vendorItem = VendorItemDAO.getInstance().create();
        
        
        /*
        VendorItemDAO.getInstance().save(vendorItem);
        vendorItem.setCost(29.99);
        //vendorItem.setVendor(vendor);
        VendorItemDAO.getInstance().save(vendorItem);
        vendorItem = VendorItemDAO.getInstance().read(vendorItem.getViguid());
        System.out.println("Cost" +vendorItem.getCost());
        */
      Product product = ConceptualDAO.getInstance().create();
        ConceptualDAO.getInstance().save(product);
        
        System.out.println("Conceptual product saved in the database");
        
        Conceptual p = (Conceptual)product;
        p.setId("LJG87YU");
        p.setPrice(35.66);
        p.setDescription("nice camera");
        p.setAvgcost(5.50);
        p.setProdname("Camera");
        ConceptualDAO.getInstance().save((Product)p);
      //Product product = ProductDAO.getInstance().create();
      //product.setPrice(18.75);
      //System.out.println("Product: " + product.getPrice());
      
      Store store = StoreDAO.getInstance().create();
      store.setAddress("456 New Street");
      store.setManager(steve);
      store.setPhone("8014984327");
      store.setLocation("Sandy");
      StoreDAO.getInstance().save(store);
      Cache.getInstance().clear();
      store = StoreDAO.getInstance().read(store.getId());
      System.out.println("Store: " + store.getAddress() +"\n Manager: " + store.getManager().getFirstName() +" " + store.getManager().getLastName() +"\n Phone: "+ store.getPhone() +"\n Store Location: "+ store.getLocation());
      
      System.out.println("*****************************************************\n*****  Start Transaction\n************************************************");
      
      
      //Create Transaction
      Transaction t = TransactionDAO.getInstance().create();
      
      //Link to customer
      t.setCustomer(cust);
      t.setType("sale");
      
      //Link to employee
      t.setEmployee(steve);
      t.setStore(store);


      //still have to figure out tax, subtotal, total, and payment
      
      
      
      
      // Store, etc.
      
      //2 Create transactionLine, attach to transaction
      TransactionLine tl = TransactionLineDAO.getInstance().create();
      tl.setTransaction(t);
      
      TransactionLine tl2 = TransactionLineDAO.getInstance().create();
      tl2.setTransaction(t);
      
      
      //3 Populate transaction line, link to rs, coupon
      RevenueSource rs = RevenueSourceFactory.getInstance().getDAO("sale").read(sale.getId());
      tl.setRevenueSource(rs);
      
      Coupon coup = CouponDAO.getInstance().read("9879NB");//needs a GUID
      tl2.setCoupon(coup);
      
      List txlines = new LinkedList();
      txlines.add(tl);
      txlines.add(tl2);
      
      t.setTxLinesList(txlines);
      
      // Calculate subtotal, tax, total
      double subtotal = t.getSubtotal();
      double tax = t.getTax();
      double total = t.getTotal();
      
      //4  Record payment info, returned change
      Payment payment = PaymentDAO.getInstance().create();
      payment.setAmount(40.00);
      payment.setChange(0.00);
      payment.setId("876JU987");
      payment.setType("Credit");
      System.out.println("Amount: " + payment.getAmount()+ "\n Change:" + payment.getChange() + "\n ID:" + payment.getId() + "\n Type: " + payment.getType());
      
      t.setPayment(payment);
      t.setStatus("complete");
      
      
      //Print and view
      
      
      //5  Process return transaction
      
      Transaction returnTrans = TransactionDAO.getInstance().create();
      returnTrans.setEmployee(steve);
      returnTrans.setCustomer(cust);
      returnTrans.setType("return");
      returnTrans.setStore(store);
      returnTrans.setTxLinesList(txlines);
      

      //6  Hold/ resume transaction
      t.setStatus("pending");
      
      // Void transaction
      t.setStatus("void");
      
      
      // Update Controller
      
      //process a sale
      
     
    }catch(Exception e) {
      e.printStackTrace();
    }
    
    
    
    
  }//main
}//class
